-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Align preallocationSize behavior #58726
Conversation
* file length should always be set * file content should always be zeroed * OpenOrCreate can't shrink an existing file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise, LGTM. Thanks.
src/libraries/System.IO.FileSystem/tests/FileStream/ctor_options_as.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.FileSystem/tests/FileStream/ctor_options_as.cs
Outdated
Show resolved
Hide resolved
This will be a behavioral change on Windows. We should either get this into .NET 6 or reconsider whether this is a break we want to take in .NET 7. |
It will, but for the good reasons (same behavior on every platform).
I think that we should include it in .NET 6, as currently nobody is using this API (because it's very new) and we avoid confusion (different behavior on different platforms) and introducing breaking changes in .NET 7. |
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Outdated
Show resolved
Hide resolved
I agree. My point is... I suggest you get buy-off about getting this into .NET 6 before merging it into .NET 7. |
ba7f512
to
81fbd92
Compare
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Outdated
Show resolved
Hide resolved
=> preallocationSize > 0 | ||
&& (access & FileAccess.Write) != 0 | ||
&& mode != FileMode.Open && mode != FileMode.Append; | ||
&& mode != FileMode.Open && mode != FileMode.Append | ||
&& (mode != FileMode.OpenOrCreate || fileHandle.CanSeek && RandomAccess.GetFileLength(fileHandle) == 0); // allow to extend only new files |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: please add parens around the latter expression... precedence between || and && is hard to remember.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just because a file length is 0 doesn't mean it's new.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just because a file length is 0 doesn't mean it's new.
I agree, but I am not sure if there is a reliable way of telling that given file is new.
src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Stephen Toub <[email protected]>
/backport to release/6.0 |
Started backporting to release/6.0: https://github.com/dotnet/runtime/actions/runs/1233431311 |
Co-authored-by: Stephen Toub <[email protected]>
…vior. (#59532) * Align preallocationSize behavior (#58726) Co-authored-by: Stephen Toub <[email protected]> * File preallocationSize: align Windows and Unix behavior. (#59338) * File preallocationSize: align Windows and Unix behavior. This aligns Windows and Unix behavior of preallocationSize for the intended use-case of specifing the size of a file that will be written. For this use-case, the expected FileAccess is Write, and the file should be a new one (FileMode.Create*) or a truncated file (FileMode.Truncate). Specifing a preallocationSize for other modes, or non-writable files throws ArgumentException. The opened file will have a length of zero, and is ready to be written to by the user. If the requested size cannot be allocated, an IOException is thrown. When the OS/filesystem does not support pre-allocating, preallocationSize is ignored. * fix pal_io preprocessor checks * pal_io more fixes * ctor_options_as.Windows.cs: fix compilation * Update tests * tests: use preallocationSize from all public APIs * pal_io: add back FreeBSD, fix OSX * tests: check allocated is zero when preallocation is not supported. * Only throw for not enough space errors * Fix compilation * Add some more tests * Fix ExtendedPathsAreSupported test * Apply suggestions from code review Co-authored-by: David Cantú <[email protected]> * Update System.Private.CoreLib Strings.resx * PR feedback * Remove GetPathToNonExistingFile * Fix compilation * Skip checking allocated size on mobile platforms. Co-authored-by: David Cantú <[email protected]> * Fix unused fileDescriptor * void fd when unused * Address feedback Co-authored-by: Adam Sitnik <[email protected]> Co-authored-by: Stephen Toub <[email protected]> Co-authored-by: Tom Deseyn <[email protected]>
Align Windows
preallocationSize
implementation to act like Unix implementation (which is less flexible and we can't make it work in other direction):FileStream.Length
)By using these benchmarks I've confirmed that there is no perf regression (to my suprise, I've double checked that)
I've also fixed an OSX bug, where we could have accidentally shrink a file opened with
FileMode.OpenOrCreate